package com.chezhujie.www.controller;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjaftersales;
import com.chezhujie.www.domain.Czjcommodity;
import com.chezhujie.www.domain.Czjspecification;
import com.chezhujie.www.domain.vo.BusinessAndPic;
import com.chezhujie.www.domain.vo.CommImg;
import com.chezhujie.www.enums.OrderType;
import com.chezhujie.www.service.CzjaftersalesService;
import com.chezhujie.www.service.CzjcommodityService;
import com.chezhujie.www.service.CzjspecificationService;
import com.chezhujie.www.utils.ConvertObj;
import com.chezhujie.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 *   商品表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjcommodity")
public class CzjcommodityController extends BaseController<Czjcommodity>{
    @Autowired
    private CzjcommodityService czjcommodityService;
    @Autowired
    private CzjspecificationService czjspecificationService;
    @Autowired
    private CzjaftersalesService czjaftersalesService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjcommodity czjcommodity, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcommodity,req,model);
        setBusinessId(model);
        setTypeId(model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjcommodity);
            Pagination pagination = czjcommodityService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取商品表列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjcommodity);
        return "czjcommodity/list";
    }
    /**
    * api接口商品表列表
    * @param pageNo
    * @param pageSize
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, Czjcommodity czjcommodity,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjcommodity);
            Pagination pagination = czjcommodityService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取商品表列表失败!"+e,"",resp);
        }

    }


    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjcommodity czjcommodity,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjcommodity.getId())){
                czjcommodityService.update(czjcommodity);


            }else{
                czjcommodity.setId(OrderType.COMMODITY.getIndex()+UUIDUtils.getUUID().substring(1,19));
                czjcommodityService.insert(czjcommodity);
            }
            model.addAttribute("success", "保存商品表成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存商品表失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("detail")
    public String detail(@RequestParam(required = true) String id,
                         Czjcommodity czjcommodity, HttpServletRequest req,HttpSession httpSession,
                         HttpServletResponse resp, ModelMap model) {
        setBusinessId(model);
        setTypeId(model);
        try {
            // TODO 商品ID
            //id = "ySn781TFucmBTBRLhko";
            BusinessAndPic entity = czjcommodityService.getCommodity(id);
            model.addAttribute("entity", entity);
            List<CommImg> pic1 = new ArrayList<CommImg>(); // 0商品图片
            List<CommImg> pic2 = new ArrayList<CommImg>(); // 1商品默认图片
            List<CommImg> pic3 = new ArrayList<CommImg>();// 3商品文字介绍
            if (null != czjcommodity
                    && null != entity.getCzjcommodityimages()
                    && entity.getCzjcommodityimages().size() > 0) {
                for (int i = 0; i < entity.getCzjcommodityimages().size(); i++) {
                    CommImg commImg=new CommImg();
                    if (entity.getCzjcommodityimages().get(i).getPicType() == 0) {

                        commImg.setImgId(entity.getCzjcommodityimages().get(i)
                                .getId());
                        commImg.setImagePath(entity.getCzjcommodityimages().get(i)
                                .getImagePath());
                        pic1.add(commImg);
                    }
                    if (entity.getCzjcommodityimages().get(i).getPicType() == 1) {
                        commImg.setImgId(entity.getCzjcommodityimages().get(i)
                                .getId());
                        commImg.setImagePath(entity.getCzjcommodityimages().get(i)
                                .getImagePath());
                        pic2.add(commImg);
                    }
                    if (entity.getCzjcommodityimages().get(i).getPicType() == 3) {
                        commImg.setImgId(entity.getCzjcommodityimages().get(i)
                                .getId());
                        commImg.setImagePath(entity.getCzjcommodityimages().get(i)
                                .getImagePath());
                        pic3.add(commImg);
                    }
                }

            }

            //商品规格
            List<Czjaftersales> czjaftersales=czjaftersalesService.getAfterByCommid(id);
            model.addAttribute("czjaftersales", czjaftersales);
            //商品售后服务
            List<Czjspecification>czjspecification=czjspecificationService.getSpecByCommid(id);
            model.addAttribute("czjspecification", czjspecification);

            model.addAttribute("pic1", pic1);
            model.addAttribute("pic2", pic2);
            model.addAttribute("pic3", pic3);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询商品表出错!详情：" + e.getMessage());
        }
        return "czjcommodity/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjcommodity czjcommodity, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcommodity,req,model);
        setBusinessId(model);
        setTypeId(model);
        if(StringUtils.isNotBlank(czjcommodity.getId())){
            czjcommodity = czjcommodityService.queryById(czjcommodity.getId());
            model.addAttribute("entity", czjcommodity);
        }
        return "czjcommodity/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String id,Czjcommodity czjcommodity, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjcommodityService.deleteById(czjcommodity.getId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setBusinessId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listBusinessId = czjcommodityService.getListBusinessId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listBusinessId", listBusinessId);
    }
    private void setTypeId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listTypeId = czjcommodityService.getListTypeId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listTypeId", listTypeId);
    }
    /** 审批 **/
    @RequestMapping("/approve.do")
    public String approve(@RequestParam(required = true)String id,
                          @RequestParam(required = true)Integer approveStatus,
                          HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model){

        czjcommodityService.changeApprove(id,approveStatus);
        return "redirect:detail.do?id="+id;
    }

    private void setMsg(Czjcommodity czjcommodity,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjcommodity);
    }
}
